using System;
using System.Collections.Generic;
using System.Text;

namespace Acms.AcmsControls
{
    public abstract class TemplateUserControl : System.Web.UI.UserControl
    {
        public TemplateUserControl()
            : base()
        {
            RequiresDataBinding = true;
        }

        public virtual AcmsContext AcmsContext{ get; set; }

        protected override void OnLoad(EventArgs e)
        {
            if (AcmsContext != null)
            {
                Page.RegisterPost(AcmsContext.CurrentPost);
            }
            base.OnLoad(e);
        }

        public bool RequiresDataBinding { get; set; }

        public override void DataBind()
        {
            RequiresDataBinding = false;
            if (AcmsContext != null)
            {
                Page.RegisterPost(AcmsContext.CurrentPost);
            }
            base.DataBind();
        }

        protected override void OnPreRender(EventArgs e)
        {
            if (RequiresDataBinding)
            {
                DataBind();
            }
            base.OnPreRender(e);
        }

        protected override object SaveViewState()
        {
            ViewState["AcmsContext"] = AcmsContext;
            ViewState["RequiresDataBinding"] = RequiresDataBinding;
            return base.SaveViewState();
        }

        protected override void LoadViewState(object savedState)
        {
            base.LoadViewState(savedState);
            AcmsContext = ViewState["AcmsContext"] as AcmsContext;
            if (ViewState["RequiresDataBinding"] is bool)
            {
                RequiresDataBinding = (bool)ViewState["RequiresDataBinding"];
            }
        }

        public Acms.Bll.PostControl FindPostControl(string name)
        {
            if (AcmsContext.CurrentPost != null)
            {
                return AcmsContext.CurrentPost.FindPostControl(name);
            }
            else
            {
                return null;
            }
        }

        public string HighlightText
        {
            get { return ViewState["HighlightText"] as string; }
            set { ViewState["HighlightText"] = value; }
        }

        protected string Highlight(string text)
        {
            if (text != null && HighlightText != null)
            {
                // Setup the regular expression and add the Or operator.
                System.Text.RegularExpressions.Regex regExp = new System.Text.RegularExpressions.Regex(string.Concat("\\b", HighlightText.Replace(" ", "|").Trim()), System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                // Highlight keywords by calling the delegate each time a keyword is found.
                return regExp.Replace(text, new System.Text.RegularExpressions.MatchEvaluator(ReplaceKeyWords));
            }
            else
            {
                return text;
            }
        }

        private string ReplaceKeyWords(System.Text.RegularExpressions.Match m)
        {

            return "<span class=\"searchHighlight\">" + m.Value + "</span>";

        }

        public new TemplatePage Page
        {
            get { return base.Page as TemplatePage; }
        }

    }
}
